<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>检索算法--二分算法</title>
</head>
<body>
<script src="search.js"></script>
<script>
    //二分查找算法
    function bindSearch(arr,data){
        var upper=arr.length-1;
        var lower=0;
        while(lower<=upper){
            var mid=Math.floor((upper+lower)/2);
            //如果中间的值小于要查找的数据，哪么让上线等于mid，反之让下限等于mid
            console.log("当前中点："+mid);
            if(arr[mid]<data){
                lower=mid+1;
            }else if(arr[mid]>data){
                upper=mid-1;
            }else{
                return mid;
            }
        }
        return -1;
    }

    var arr=[2,4,5,7,8,9,10,21];
    if(bindSearch(arr,8)){
        disArr(arr);
    }
</script>
</body>
</html>